// pages/com.js
let app = getApp();
let util = require("../../utils/util");
let common = require("../../common/common");

Page({
    /**
     * 组件的初始数据
     */
    data: {
        avatarUrl: "",
        nickName: "",
        name: "",
        phone: "",
        industry: "",
        company: "",
        address: "",
        introduce: "",
        business: [{name: "", intro: "", showImg: []}], // 填写业务整个组件, length表示当前的组件个数

        editIconUrl: "../../image/edit.png",
        editCardUrl: "../editCard/index",
        myCardInfoNameList: ["公司", "职务", "微信", "地址"],
        myCardInfoValueList: ["", "", "", ""],

        isCheckClick: false,
        isFavoriteClick: false,
        isLikeClick: false,

        // interval: 3000, // swiper 自动切换间隔
        checkIconUrl: "../../image/check.png",  // 查看
        favoriteIconUrl: "../../image/favorite.png", // 收藏
        likeIconUrl: "../../image/like.png", // 点赞

        checkCnt: 500,
        favoriteCnt: 10,
        likeCnt: 7,

        mapIconList: [
            "", "", "", "../../image/mapIcon.png"
        ],

        token: "",
    },

    checkClick: function (e) {
        let that = this;
        let list = wx.getStorageSync("list")[0];
        console.log("\n\n*****\nlist: ", list);

        // this.setData({
        //     isCheckClick: !that.data.isCheckClick
        // });
        //
        //
        // if (this.data.isCheckClick) {
        //     this.setData({
        //         checkIconUrl: "../../image/check_red.png",
        //     });
        // } else {
        //     this.setData({
        //         checkIconUrl: "../../image/check.png",
        //     });
        // }

        this.setData({
            checkIconUrl: "../../image/check_red.png",
        });

        if (list) {
            wx.request({
                url: common.domain + common.prefixUrl + "/check",
                method: "POST",
                data: {
                    check: list._id,
                    openid: list.openId,
                },
                success: res => {
                    that.setData({
                        checkCnt: that.data.checkCnt + 1,
                    });
                }
            });
        }

    },

    previewImage: function (e) {
        console.log(e);
        let currentUrl = e.target.dataset.src; // 选中的图片url
        let idx = e.currentTarget.dataset.index; // business index

        wx.previewImage({
            current: currentUrl,
            urls: this.data.business[idx].showImg,
        });
    },

    favoriteClick: function (e) {
        let list = wx.getStorageSync("list")[0];
        let that = this;
        this.setData({
            isFavoriteClick: !that.data.isFavoriteClick
        });


        if (this.data.isFavoriteClick) {
            this.setData({
                favoriteIconUrl: "../../image/favorite_red.png",
            });
        } else {
            this.setData({
                favoriteIconUrl: "../../image/favorite.png",
            });
        }

        wx.request({
            url: common.domain + common.prefixUrl + "/favorite",
            method: "POST",
            data: {
                favorite: list._id,
                openid: list.openId,
            },
            success: res => {
                console.log("\n\n*****\nres: ", res);
                that.setData({
                    favoriteCnt: res.favorite.length,
                });
            }
        });
    },

    likeUrlClick: function (e) {
        let list = wx.getStorageSync("list")[0];
        console.log("\n\n*****\nlist: ", list);
        let that = this;

        this.setData({
            isLikeClick: !that.data.isLikeClick
        });


        if (this.data.isLikeClick) {
            this.setData({
                likeIconUrl: "../../image/like_red.png",
            });
        } else {
            this.setData({
                likeIconUrl: "../../image/like.png",
            });
        }

        wx.request({
            url: common.domain + common.prefixUrl + "/like",
            method: "POST",
            data: {
                like: list._id,
                openid: list.openId,
            },
            success: res => {
                console.log("\n\n*****\nres: ", res);
                that.setData({
                    likeCnt: res.like.length,
                });
            }
        });
    },

    location: function () {
        let latitude = wx.getStorageSync("latitude");
        let longitude = wx.getStorageSync("longitude");

        wx.openLocation({
            latitude: latitude,
            longitude: longitude,
        });
    },

    onLoad: function () {
        console.log(" in my card -> onLoad...");
        let that = this;
        wx.getSystemInfo({
            success: function (res) {
                console.log("_sys-res: ", res);
            }
        });

        wx.showShareMenu({
            withShareTicket: true
        });

        that.checkClick();
    },

    onShow: function () {
        let openId = wx.getStorageSync("openId");
        let that = this;

        console.log(" in myCard -> onShow... ");
        console.log("");
        console.log(" in myCard -> onShow openId: ", openId);

        if (openId) {

            wx.request({
                url: common.domain + common.prefixUrl,
                method: "GET",
                header: {
                    "content-type": "application/json"
                },
                data: {
                    cond: JSON.stringify({openId: openId})
                },
                success: res => {
                    console.log("  ____ get list res", res);
                    console.log(res.data.data.list);
                    wx.setStorageSync("list", res.data.data.list);
                    let list = res.data.data.list;
                    list = list[0];

                    if (list && list.avatarUrl) {
                        // 已经创建名片
                        let address = list.address.province + list.address.city + list.address.area + list.address.detail;

                        let urlData = util.qqMapUrlDegree(address);
                        console.log("urlData: ", urlData);

                        wx.request({
                            url: urlData,
                            method: "GET",
                            header: {
                                "content-type": "application/json"
                            },
                            success: res => {
                                let location = res.data.result.location;
                                wx.setStorageSync("latitude", location.lat);
                                wx.setStorageSync("longitude", location.lng);

                                console.log(urlData);
                                console.log("in myCard latitude: ", location.lat);

                                that.setData({
                                    latitude: location.lat,
                                    longitude: location.lng,
                                });
                            }
                        });

                        that.setData({
                            avatarUrl: list.avatarUrl,
                            name: list.name,
                            phone: list.phone,
                            myCardInfoValueList: [list.company, list.position, list.wechat, address],
                            industry: list.industry,
                            introduce: list.introduce,
                            business: list.business,
                            checkCnt: list.check.length,
                            favoriteCnt: list.favorite.length,
                            likeCnt: list.like.length,
                        });
                    } else {
                        // 未创建名片
                        console.log("  ______ no create card!");
                        wx.getLocation({
                            type: "gcj02", //返回可以用于wx.openLocation的经纬度
                            success: function (res) {
                                let latitude = res.latitude;
                                let longitude = res.longitude;
                                console.log("latitude: ", latitude);
                                console.log("longitude: ", longitude);

                                wx.setStorageSync("latitude", res.latitude);
                                wx.setStorageSync("longitude", res.longitude);

                                // let urlData = util.qqMapUrlCN(latitude, longitude);
                                // console.log("urlData: ", urlData);

                                // wx.request({
                                //     url: urlData,
                                //     method: "GET",
                                //     header: {
                                //         "content-type": "application/json"
                                //     },
                                //     success: res => {
                                //         console.log(
                                //             "  ___res.data.result.address_component: ",
                                //             res.data.result.address_component
                                //         );
                                //         let addressComponent = res.data.result.address_component;
                                //         wx.setStorageSync("address", [
                                //             addressComponent.province,
                                //             addressComponent.city,
                                //             addressComponent.district
                                //         ]);
                                //     }
                                // });
                                // wx.setStorage("latitude", res.latitude);
                                // wx.setStorageSync("longitude", res.longitude);
                            },
                            cancel: function (res) {
                                wx.showModal({
                                    title: "",
                                    content: "请允许获取您的地理位置！"
                                });
                            },
                            fail: function (res) {
                                wx.showModal({
                                    title: "",
                                    content: "没有获得位置权限！"
                                });
                            }
                        });
                    }
                }
            });
        } else {
            console.log("\n\n*****\napp.getUserInfo(): ", app.getUserInfo());
        }
    },

    onReady: function () {
        // Do something when page ready.
        console.log(" in myCard -> onReady...");
    },

    onShareAppMessage: function (res) {
        if (res.from === "button") {
            // 来自页面内转发按钮
            console.log(res.target);
        }
        return {
            title: "(*^_^*) 这是我的名片，望惠存！~~",
            path: "/pages/myCard/index",
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        };
    }
});
